package com.mall.user.service.impl;

import com.central.common.utils.GlobalUserContext;
import com.mall.entity.SecurityUser;
import com.mall.entity.Users;
import com.mall.user.service.PermissionService;
import com.mall.user.service.UserCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserCheckService userCheckService;

    @Autowired
    private PermissionService permissionService;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询数据
        Users user = userCheckService.getUser(username);
        //判断
        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //根据用户查询用户权限列表
        List<String> permissionValueList = permissionService.selectPermissionValueByUsername(user.getUsername());
        SecurityUser securityUser = new SecurityUser();
        securityUser.setCurrentUserInfo(user);
        securityUser.setPermissionValueList(permissionValueList);
        GlobalUserContext.setUser(user);
        return securityUser;
    }
}
